export class Node {
	constructor(element) {
		this.element = element;//保存元素
		this.next = null;//指向下一个节点
	}
}

export class LinkedList {
	constructor() {
		this.head = null;
		this.length = 0;
	}

	//append(element): 向列表尾部添加一个新的项
	append(element) {
		//1.根据element创建Node对象
		const newNode = new Node(element);
		//2.追加到最后
		if (!this.head) {//当head为null的时候				this.head === null
			this.head = newNode;//直接让head等于node节点
		} else {//当head不为null的时候
			let current = this.head;
			while (current.next) {
				current = current.next;//把head.next赋值给current，循环直到current.next 为空值 把newNode赋值给此时的current.next
			}
			current.next = newNode;
		}
		this.length++;	//不管什么情况，length自增1
	}

	//insert(position,element): 向列表的特定位置插入一个新的项
	insert(position, element) {
		//1.判断越界问题
		if (position < 0 || position > this.length) return false;
		//2.没有越界，创建
		const newNode = new Node(element); //创建一个节点newNode
		//3.插入元素
		if (position === 0) {
			newNode.next = this.head;	//将当前的节点头 赋给当前节点的下一个节点
			this.head = newNode;		//将当前的节点赋给当前的头节点
		} else {
			let index = 0;	//用于循环自增判断在哪儿进行插入操作
			let current = this.head;//将当前的节点赋给current进行循环
			let previous = null;	//进行插入的元素的前一个节点位置
			while (index++ < position) {
				previous = current;	//把current赋予给previous
				current = current.next;//current变成当前current的下一个节点
			}
			previous.next = newNode;//将previous的下一个next指向要插入的newNode节点
			newNode.next = current;//将newNode的next的指向给current
		}
		this.length++;
		return true;
	}

	//get(position): 获取对应位置的元素
	get(position) {
		//1.判断越界问题
		if (position < 0 || position > this.length - 1) return null;
		//2.查找该位置的元素
		let index = 0;
		let current = this.head;
		while (index++ < position) {
			current = current.next;
		}
		return current.element;
	}


	//indexOf(element): 返回元素在列表中的索引。如果列表中没有该元素则返回-1
	indexOf(element) {
		//1.获取第一个元素
		let index = 0;
		let current = this.head;
		//2.开始查找
		while (current) {
			if (current.element === element) {
				return index;
			}
			index++;
			current = current.next;
		}
		return -1;
	}

	//uodate(position, element)：修改某个位置的元素
	update(position, element) {
		//判断越界问题
		if (position < 0 || position > this.length - 1) return false;
		//1.先删除位置上的元素
		const result = this.removeAt(position);
		//2。插入该位置上的元素
		this.insert(position, element);
		//返回更新的值
		return result;
	}

	//removeAt(position): 从列表的特定位置移除一项
	removeAt(position) {
		//1.有位置，先判断越界问题
		if (position < 0 || position > this.length - 1) return null;
		//2.删除元素
		let index = 0;
		let current = this.head;
		let previous = null;
		if (position === 0) {
			this.head = current.next;
		} else {
			while (index++ < position) {
				previous = current;		//previous为要删除的位置的前一个
				current = current.next;
			}
			previous.next = current.next;
		}
		this.length--;
		return current.element;
	}
	//remove(element): 从列表中根据元素移除一项
	remove(element) {
		const index = this.indexOf(element);
		if (index === -1) return;
		this.removeAt(index);
		
	}
	//isEmpty(): 如果链表中不包含任何元素，返回true,如果链表长度大于0则返回false
	isEmpty() {
		return this.length === 0;
	}
	//size(): 返回链表包含元素个数。与数组的length属性类似
	size() {
		return this.length;
	}
}
